#include "Game.h"

#include "../Utility/Utils.h"
#include "InputManager.h"
#include "GraphicManager.h"

extern tActionMapping kaActionMapping[];

// Function to initialize the game
bool cGame::Init() {
	mLevel.Init(ksPathMap);
	mbFinished = false;
	// Initializing the '0'
	mfX = 0.0f;
	mfY = 0.0f;
	mfVX = 10;
	mfVY = 10;
	cInputManager::Get().Init(kaActionMapping, eIA_Count);
	cGraphicManager::Get().Init();
	cGraphicManager::Get().ShowTheCursor(false);
	return true;
}

// Function to uptade the game
void cGame::Update(float lfTimeStep) {
	// Update the input manager
	cInputManager::Get().Update(lfTimeStep);
	mLevel.Update(lfTimeStep);

	const cInputAction& lAction = cInputManager::Get().GetAction( eIA_CloseApplication);
	if (lAction.GetIsPressed()) {
		mbFinished = true;
		return;
	}
}

// Function to render the game
void cGame::Render() {	
	// Get the graphicManager
	cGraphicManager & lGraphic = cGraphicManager::Get();
	lGraphic.ClearBuffer();
	// Render de level
	mLevel.Render();
	// Swap buffers
	lGraphic.SwapBuffer();	
}

// Function to deinitialize the game
bool cGame::DeInit() {
	cInputManager::Get().DeInit();
	cGraphicManager::Get().ShowTheCursor(true);
	cGraphicManager::Get().DeInit();
	mLevel.DeInit();
	return true;
}